package com.edu.zhku.wms.service;

import cn.hutool.core.util.StrUtil;
import com.edu.zhku.wms.dto.LoginRequest;
import com.edu.zhku.wms.mapper.EmployeeMapper;
import com.edu.zhku.wms.mapper.UserMapper;
import com.edu.zhku.wms.pojo.Employee;
import com.edu.zhku.wms.pojo.EmployeeExample;
import com.edu.zhku.wms.pojo.User;
import com.edu.zhku.wms.utils.JwtUtil;
import com.edu.zhku.wms.vo.UserInfo;
import com.edu.zhku.wms.vo.UserPrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Feng
 * @since 2021/10/28 13:59
 */
@Service
public class UserPrincipalService implements UserDetailsService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名为" + username + "的用户不存在！");
        }
        return UserPrincipal.create(user);
    }

    public String login(LoginRequest loginRequest) {
        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        String jwt = jwtUtil.createJWT(authentication, loginRequest.getRememberMe());
        return jwt;
    }

    public void logout(HttpServletRequest request) {
        // 设置JWT过期
        jwtUtil.invalidateJWT(request);
    }

    public UserInfo getLoggedInfo(HttpServletRequest request) {
        String jwt = jwtUtil.getJwtFromRequest(request);
        if (StrUtil.isBlank(jwt)) {
            return null;
        }
        User user = userMapper.selectByUsername(jwtUtil.getUsernameFromJWT(jwt));
        if (user == null) {
            return null;
        }
        Employee employee = null;
        EmployeeExample employeeExample = new EmployeeExample();
        employeeExample.createCriteria().andUserIdEqualTo(user.getId());
        List<Employee> employees = employeeMapper.selectByExample(employeeExample);
        if (!employees.isEmpty()) {
            employee = employees.get(0);
        }
        return new UserInfo(user, employee);
    }
}
